﻿// ===============================================================================
// Project Name        :    Gfw.BLL.Basic.Stock.Facilities
// Project Description :    
// ===============================================================================
// Class Name          :    ToStockMoneyFacility
// Namespace:          :    Gfw.BLL.Basic.Stock.Facilities
// Class Version       :    v1.0.0.0
// Class Description   :    
// CLR Version:        :    4.0.30319.17626
//Author               :    lif
// Machine             :    ADICO-PC
// Create Time         :    2012/7/16 22:01:51
// Update Time         :    2012/7/16 22:01:51
// ===============================================================================
// Copyright © lif 2012 . All rights reserved.
// ===============================================================================

using System;
using Gfw.Infrastructure.Account;
using Gfw.Infrastructure.ExceptionImpl;
using Gfw.Infrastructure.Facilities;
using Gfw.Models.Account;
using Gfw.Models.Stock;
using Gfw.Repositories.Account;
using Gfw.Repositories.Stock;
using Gfw.Repository;

namespace Gfw.Infrastructure.Stock.Facilities
{
    public class ToStockMoneyFacility : IFacility
    {
        protected string _userId;
        protected ActUserAccountinformation _userAccount;
        protected decimal _count;

        public ToStockMoneyFacility(ActUserAccountinformation userAccount, decimal count)
        {
            _count = count;
            _userAccount = userAccount;

            if (count <= 0)
                throw new GfwVerifyException("转账的参数不正确！");
            _count = count;
            _userAccount = userAccount;

        }

        public ToStockMoneyFacility(string userId, decimal count)
        {
            if (count <= 0)
                throw new GfwVerifyException("转账的参数不正确！");

            _userId = userId;
            _count = count;
        }

        public virtual void Execute(Repository.UnitOfWork unitOfWork = null)
        {
            UnitOfWork work = unitOfWork ?? new UnitOfWork();
            ActUserAccountinformationRepository accountRep = null;
            if (_userAccount == null)
            {
                accountRep = new ActUserAccountinformationRepository();
                _userAccount = accountRep.Get(_userId, work);
            }

            if (_userAccount.ElectronicMoney < _count)
                throw new GfwVerifyException("余额不足!");

            UserAccountRepository stockRep = new UserAccountRepository();
            var stockAccount = stockRep.Get(_userAccount.Userid, work);

            stockAccount.StockMoney += _count;
            _userAccount.ElectronicMoney -= _count;


            DateTime tradeDate = DateTime.Now;
            ActUserBargain4emoney emoneyHistory = new ActUserBargain4emoney()
                                                      {
                                                          Id = Guid.NewGuid().ToString(),
                                                          AwardSerialNo = "",
                                                          Balance = _userAccount.ElectronicMoney,
                                                          Income = 0,
                                                          Outlay = _count,
                                                          Taxation = 0,
                                                          TaxationSerialNo = "",
                                                          TradeAbstract = "D币转G币",
                                                          TradeDate = tradeDate,
                                                          TradeType = (int) UserTradeTypeEnum.TransferOut,
                                                          Userid = _userAccount.Userid

                                                      };

            StkRecharge recharge = new StkRecharge()
                                       {
                                           Id = Guid.NewGuid().ToString(),
                                           Income = _count,
                                           Outcome = 0,
                                           RechargeAbstract = "D币转G币",
                                           RechargeDate = tradeDate,
                                           RechargeType = (int) UserTradeTypeEnum.TransferIn,
                                           Remaining = stockAccount.StockMoney,
                                           Userid = _userAccount.Userid
                                       };
            UserRechargeRepository reRep = new UserRechargeRepository();
            ActUserBargain4emoneyRepository emnoeyRep = new ActUserBargain4emoneyRepository();

            stockRep.Update(stockAccount, work);
            reRep.Save(recharge, work);
            emnoeyRep.Save(emoneyHistory, work);
            //作为单事务，需要提交更改
            if (unitOfWork == null)
            {
                accountRep.Update(_userAccount, work);
                work.Commit();
            }
        }
    }
}
